// 引入相关服务
const departmentService = require('../../services/department');
const hospitalService = require('../../services/hospital');
const consultService = require('../../services/consult');

Page({
  data: {
    // 轮播图数据
    banners: [
      { id: 1, imageUrl: '/static/images/banner1.jpg' },
      { id: 2, imageUrl: '/static/images/banner2.jpg' },
      { id: 3, imageUrl: '/static/images/banner3.jpg' }
    ],
    
    // 科室数据
    departments: [],
    
    // 医生数据
    doctors: [],
    
    // 健康知识文章
    articles: [
      {
        id: 1,
        title: '宠物猫日常护理指南',
        description: '养猫新手必看的日常护理指南，包括喂食、洗澡、梳毛等基础知识。',
        coverUrl: '/static/images/article1.jpg',
        date: '2023-05-10',
        views: 1289
      },
      {
        id: 2,
        title: '宠物狗常见疫苗接种时间表',
        description: '详细介绍宠物狗各阶段需要接种的疫苗以及注意事项。',
        coverUrl: '/static/images/article2.jpg',
        date: '2023-05-08',
        views: 987
      },
      {
        id: 3,
        title: '如何正确给宠物喂药？',
        description: '宠物生病需要服药时，这些小技巧可以帮助你轻松给宠物喂药。',
        coverUrl: '/static/images/article3.jpg',
        date: '2023-05-05',
        views: 752
      }
    ]
  },
  
  onLoad: function() {
    this.loadDepartments();
    this.loadDoctors();
  },
  
  // 加载科室数据
  loadDepartments: function() {
    wx.showLoading({
      title: '加载中...',
    });
    
    departmentService.getDepartmentList(1, 10)
      .then(res => {
        this.setData({
          departments: res.records
        });
        wx.hideLoading();
      })
      .catch(err => {
        console.error('获取科室列表失败', err);
        wx.hideLoading();
      });
  },
  
  // 加载医生数据
  loadDoctors: function() {
    hospitalService.getStaffList(null, 1, 3)
      .then(res => {
        // 添加部门名称
        const doctors = res.records.map(doctor => {
          const department = this.data.departments.find(d => d.id === doctor.departmentId);
          return {
            ...doctor,
            departmentName: department ? department.name : '未知科室'
          };
        });
        
        this.setData({
          doctors
        });
      })
      .catch(err => {
        console.error('获取医生列表失败', err);
      });
  },
  
  // 通用跳转方法
  navigateTo: function(e) {
    const url = e.currentTarget.dataset.url;
    
    // 检查是否是tabBar页面
    const tabBarPages = [
      '/pages/index/index',
      '/pages/appointment/index',
      '/pages/consult/index',
      '/pages/user/index'
    ];
    
    if (tabBarPages.includes(url)) {
      // 如果是tabBar页面，使用switchTab
      wx.switchTab({
        url
      });
    } else {
      // 如果不是tabBar页面，使用navigateTo
      wx.navigateTo({
        url
      });
    }
  },
  
  // 跳转到科室详情
  navigateToDepartment: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/department/detail?id=${id}`
    });
  },
  
  // 跳转到医生详情
  navigateToDoctor: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/hospital/staff?id=${id}`
    });
  },
  
  // 跳转到搜索页面
  navigateToSearch: function() {
    wx.navigateTo({
      url: '/pages/search/index'
    });
  },
  
  // 创建会话
  createConsult: function(e) {
    const staffId = e.currentTarget.dataset.id;
    
    wx.showLoading({
      title: '创建会话中...',
    });
    
    consultService.createConversation(staffId)
      .then(res => {
        wx.hideLoading();
        const conversationId = typeof res === 'object' ? res.id : res;
        
        // 默认状态为进行中(0)，因为这是新创建的会话
        const conversationStatus = 0;
        console.log('新创建的会话，状态强制设置为进行中(0)，会话ID:', conversationId);
        
        wx.navigateTo({
          url: `/pages/consult/chat?id=${conversationId}&status=${conversationStatus}`,
          fail: (err) => {
            console.error('无法跳转到聊天页面:', err);
            wx.showToast({
              title: '跳转聊天页面失败',
              icon: 'none'
            });
          }
        });
      })
      .catch(err => {
        console.error('创建会话失败', err);
        wx.hideLoading();
        wx.showToast({
          title: '创建会话失败，请重试',
          icon: 'none'
        });
      });
  },
  
  // 顶部科室导航"查看全部"的跳转
  navigateToAllDepartments: function() {
    wx.navigateTo({
      url: '/pages/department/index'
    });
  },
  
  onShareAppMessage: function() {
    return {
      title: '宠物医院 - 专业的宠物医疗平台',
      path: '/pages/index/index'
    };
  }
}); 